package org.jeecg.modules.yfYqgk.service.impl;

import org.jeecg.modules.yfYqgk.entity.HqYfYqgk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkZyry;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkTudcq;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkLzqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkGhqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkHqqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkXfaqqk;
import org.jeecg.modules.yfYqgk.entity.HqYfYqgkSdqrgyjxhqk;
import org.jeecg.modules.yfYqgk.mapper.HqYfYqgkZyryMapper;
import org.jeecg.modules.yfYqgk.mapper.HqYfYqgkTudcqMapper;
import org.jeecg.modules.yfYqgk.mapper.HqYfYqgkLzqkMapper;
import org.jeecg.modules.yfYqgk.mapper.HqYfYqgkGhqkMapper;
import org.jeecg.modules.yfYqgk.mapper.HqYfYqgkHqqkMapper;
import org.jeecg.modules.yfYqgk.mapper.HqYfYqgkXfaqqkMapper;
import org.jeecg.modules.yfYqgk.mapper.HqYfYqgkSdqrgyjxhqkMapper;
import org.jeecg.modules.yfYqgk.mapper.HqYfYqgkMapper;
import org.jeecg.modules.yfYqgk.service.IHqYfYqgkService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 营区概况
 * @Author: jeecg-boot
 * @Date:   2020-07-12
 * @Version: V1.0
 */
@Service
public class HqYfYqgkServiceImpl extends ServiceImpl<HqYfYqgkMapper, HqYfYqgk> implements IHqYfYqgkService {

	@Autowired
	private HqYfYqgkMapper hqYfYqgkMapper;
	@Autowired
	private HqYfYqgkZyryMapper hqYfYqgkZyryMapper;
	@Autowired
	private HqYfYqgkTudcqMapper hqYfYqgkTudcqMapper;
	@Autowired
	private HqYfYqgkLzqkMapper hqYfYqgkLzqkMapper;
	@Autowired
	private HqYfYqgkGhqkMapper hqYfYqgkGhqkMapper;
	@Autowired
	private HqYfYqgkHqqkMapper hqYfYqgkHqqkMapper;
	@Autowired
	private HqYfYqgkXfaqqkMapper hqYfYqgkXfaqqkMapper;
	@Autowired
	private HqYfYqgkSdqrgyjxhqkMapper hqYfYqgkSdqrgyjxhqkMapper;
	
	@Override
	@Transactional
	public void saveMain(HqYfYqgk hqYfYqgk, List<HqYfYqgkZyry> hqYfYqgkZyryList,List<HqYfYqgkTudcq> hqYfYqgkTudcqList,List<HqYfYqgkLzqk> hqYfYqgkLzqkList,List<HqYfYqgkGhqk> hqYfYqgkGhqkList,List<HqYfYqgkHqqk> hqYfYqgkHqqkList,List<HqYfYqgkXfaqqk> hqYfYqgkXfaqqkList,List<HqYfYqgkSdqrgyjxhqk> hqYfYqgkSdqrgyjxhqkList) {
		hqYfYqgkMapper.insert(hqYfYqgk);
		if(hqYfYqgkZyryList!=null && hqYfYqgkZyryList.size()>0) {
			for(HqYfYqgkZyry entity:hqYfYqgkZyryList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkZyryMapper.insert(entity);
			}
		}
		if(hqYfYqgkTudcqList!=null && hqYfYqgkTudcqList.size()>0) {
			for(HqYfYqgkTudcq entity:hqYfYqgkTudcqList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkTudcqMapper.insert(entity);
			}
		}
		if(hqYfYqgkLzqkList!=null && hqYfYqgkLzqkList.size()>0) {
			for(HqYfYqgkLzqk entity:hqYfYqgkLzqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkLzqkMapper.insert(entity);
			}
		}
		if(hqYfYqgkGhqkList!=null && hqYfYqgkGhqkList.size()>0) {
			for(HqYfYqgkGhqk entity:hqYfYqgkGhqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkGhqkMapper.insert(entity);
			}
		}
		if(hqYfYqgkHqqkList!=null && hqYfYqgkHqqkList.size()>0) {
			for(HqYfYqgkHqqk entity:hqYfYqgkHqqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkHqqkMapper.insert(entity);
			}
		}
		if(hqYfYqgkXfaqqkList!=null && hqYfYqgkXfaqqkList.size()>0) {
			for(HqYfYqgkXfaqqk entity:hqYfYqgkXfaqqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkXfaqqkMapper.insert(entity);
			}
		}
		if(hqYfYqgkSdqrgyjxhqkList!=null && hqYfYqgkSdqrgyjxhqkList.size()>0) {
			for(HqYfYqgkSdqrgyjxhqk entity:hqYfYqgkSdqrgyjxhqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkSdqrgyjxhqkMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HqYfYqgk hqYfYqgk,List<HqYfYqgkZyry> hqYfYqgkZyryList,List<HqYfYqgkTudcq> hqYfYqgkTudcqList,List<HqYfYqgkLzqk> hqYfYqgkLzqkList,List<HqYfYqgkGhqk> hqYfYqgkGhqkList,List<HqYfYqgkHqqk> hqYfYqgkHqqkList,List<HqYfYqgkXfaqqk> hqYfYqgkXfaqqkList,List<HqYfYqgkSdqrgyjxhqk> hqYfYqgkSdqrgyjxhqkList) {
		hqYfYqgkMapper.updateById(hqYfYqgk);
		
		//1.先删除子表数据
		hqYfYqgkZyryMapper.deleteByMainId(hqYfYqgk.getId());
		hqYfYqgkTudcqMapper.deleteByMainId(hqYfYqgk.getId());
		hqYfYqgkLzqkMapper.deleteByMainId(hqYfYqgk.getId());
		hqYfYqgkGhqkMapper.deleteByMainId(hqYfYqgk.getId());
		hqYfYqgkHqqkMapper.deleteByMainId(hqYfYqgk.getId());
		hqYfYqgkXfaqqkMapper.deleteByMainId(hqYfYqgk.getId());
		hqYfYqgkSdqrgyjxhqkMapper.deleteByMainId(hqYfYqgk.getId());
		
		//2.子表数据重新插入
		if(hqYfYqgkZyryList!=null && hqYfYqgkZyryList.size()>0) {
			for(HqYfYqgkZyry entity:hqYfYqgkZyryList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkZyryMapper.insert(entity);
			}
		}
		if(hqYfYqgkTudcqList!=null && hqYfYqgkTudcqList.size()>0) {
			for(HqYfYqgkTudcq entity:hqYfYqgkTudcqList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkTudcqMapper.insert(entity);
			}
		}
		if(hqYfYqgkLzqkList!=null && hqYfYqgkLzqkList.size()>0) {
			for(HqYfYqgkLzqk entity:hqYfYqgkLzqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkLzqkMapper.insert(entity);
			}
		}
		if(hqYfYqgkGhqkList!=null && hqYfYqgkGhqkList.size()>0) {
			for(HqYfYqgkGhqk entity:hqYfYqgkGhqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkGhqkMapper.insert(entity);
			}
		}
		if(hqYfYqgkHqqkList!=null && hqYfYqgkHqqkList.size()>0) {
			for(HqYfYqgkHqqk entity:hqYfYqgkHqqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkHqqkMapper.insert(entity);
			}
		}
		if(hqYfYqgkXfaqqkList!=null && hqYfYqgkXfaqqkList.size()>0) {
			for(HqYfYqgkXfaqqk entity:hqYfYqgkXfaqqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkXfaqqkMapper.insert(entity);
			}
		}
		if(hqYfYqgkSdqrgyjxhqkList!=null && hqYfYqgkSdqrgyjxhqkList.size()>0) {
			for(HqYfYqgkSdqrgyjxhqk entity:hqYfYqgkSdqrgyjxhqkList) {
				//外键设置
				entity.setZhubId(hqYfYqgk.getId());
				hqYfYqgkSdqrgyjxhqkMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hqYfYqgkZyryMapper.deleteByMainId(id);
		hqYfYqgkTudcqMapper.deleteByMainId(id);
		hqYfYqgkLzqkMapper.deleteByMainId(id);
		hqYfYqgkGhqkMapper.deleteByMainId(id);
		hqYfYqgkHqqkMapper.deleteByMainId(id);
		hqYfYqgkXfaqqkMapper.deleteByMainId(id);
		hqYfYqgkSdqrgyjxhqkMapper.deleteByMainId(id);
		hqYfYqgkMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hqYfYqgkZyryMapper.deleteByMainId(id.toString());
			hqYfYqgkTudcqMapper.deleteByMainId(id.toString());
			hqYfYqgkLzqkMapper.deleteByMainId(id.toString());
			hqYfYqgkGhqkMapper.deleteByMainId(id.toString());
			hqYfYqgkHqqkMapper.deleteByMainId(id.toString());
			hqYfYqgkXfaqqkMapper.deleteByMainId(id.toString());
			hqYfYqgkSdqrgyjxhqkMapper.deleteByMainId(id.toString());
			hqYfYqgkMapper.deleteById(id);
		}
	}
	
}
